
package com.ami.kvm.jviewer.hid;

import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.kvmpkts.IVTPPktHdr;
import com.ami.kvm.jviewer.kvmpkts.KMCrypt;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class USBKeyboardRep extends USBMessage {
	private int keyCode;
	private int keyLocation;
	private boolean keyPress;
	private char keyChar = '\000';
	private byte[] m_report;
	private byte[] m_encReport;
	private ByteBuffer m_repBuf;
	private KeyProcessor m_USBKeyProcessor;
	public boolean KeyBoardDataNull;
	static int SeqNum = 0;

	public USBKeyboardRep() {
		this(0, 0, true, '\000');
	}

	public USBKeyboardRep(int paramInt1, int paramInt2, boolean paramBoolean, char paramChar) {
		this.m_vHdr = new IVTPPktHdr((short) 1, 41, (short) 0);
		this.m_devType = 48;
		this.m_protocol = 16;
		this.m_dataLen = 8;
		this.m_report = new byte[49];
		this.m_encReport = new byte[49];
	}

	public void set(int paramInt1, int paramInt2, boolean paramBoolean) {
		this.keyCode = paramInt1;
		this.keyLocation = paramInt2;
		this.keyPress = paramBoolean;
	}

	public void setKeyChar(char paramChar) {
		this.keyChar = paramChar;
	}

	public byte[] report() {
		int i = 9;
		this.m_repBuf = ByteBuffer.wrap(this.m_report);
		this.m_repBuf.order(ByteOrder.LITTLE_ENDIAN);
		this.m_repBuf.position(0);

		this.m_vHdr.setSize(41);
		this.m_vHdr.setStatus((short) 0);
		this.m_repBuf.put(this.m_vHdr.array());

		this.m_repBuf.put(this.m_signature);
		this.m_repBuf.put((byte) 1);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 32);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(i);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 48);
		this.m_repBuf.put((byte) 16);
		this.m_repBuf.put((byte) Byte.MIN_VALUE);
		this.m_repBuf.put((byte) 2);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(SeqNum);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);

		this.m_repBuf.put((byte) 8);
		if (this.m_USBKeyProcessor == null) {
			this.m_USBKeyProcessor = JViewerApp.getInstance().getKeyProcesssor();
		}
		byte[] arrayOfByte = this.m_USBKeyProcessor.convertKeyCode(this.keyCode, this.keyLocation, this.keyPress,
				this.keyChar);

		if (arrayOfByte == null) {
			this.m_repBuf.put(new byte[6]);
			this.KeyBoardDataNull = true;
		} else {
			this.m_repBuf.put(arrayOfByte);
		}

		int j = 0;
		for (int k = 8; k < 40; k++) {
			j = j + (this.m_repBuf.get(k) & 0xFF) & 0xFF;
		}
		this.m_repBuf.put(19, (byte) -(byte) (j & 0xFF));

		SeqNum += 1;
		return this.m_report;
	}

	public byte[] encryptedReport(KMCrypt paramKMCrypt) {
		int i = 9;
		this.m_repBuf = ByteBuffer.wrap(this.m_encReport);
		this.m_repBuf.order(ByteOrder.LITTLE_ENDIAN);
		this.m_repBuf.position(0);

		this.m_vHdr.setSize(41);
		this.m_vHdr.setStatus((short) 255);
		this.m_repBuf.put(this.m_vHdr.array());

		this.m_repBuf.put(this.m_signature);
		this.m_repBuf.put((byte) 1);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 32);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(i);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 48);
		this.m_repBuf.put((byte) 16);
		this.m_repBuf.put((byte) Byte.MIN_VALUE);
		this.m_repBuf.put((byte) 2);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.putInt(SeqNum);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);
		this.m_repBuf.put((byte) 0);

		this.m_repBuf.put((byte) 8);

		byte[] arrayOfByte1 = this.m_USBKeyProcessor.convertKeyCode(this.keyCode, this.keyLocation, this.keyPress,
				this.keyChar);

		if (arrayOfByte1 == null) {
			return report();
		}
		byte[] arrayOfByte2 = paramKMCrypt.encrypt(arrayOfByte1);

		if (arrayOfByte2 == null) {
			return report();
		}
		this.m_repBuf.put(arrayOfByte2);

		int j = 0;
		for (int k = 8; k < 40; k++) {
			j = j + (this.m_repBuf.get(k) & 0xFF) & 0xFF;
		}
		this.m_repBuf.put(19, (byte) -(byte) (j & 0xFF));

		SeqNum += 1;
		return this.m_encReport;
	}

	public void setAutoKeybreakMode(boolean paramBoolean) {
		this.m_USBKeyProcessor.setAutoKeybreakMode(paramBoolean);
	}

	public boolean getAutoKeybreakMode() {
		return this.m_USBKeyProcessor.getAutoKeybreakMode();
	}

	public KeyProcessor getM_USBKeyProcessor() {
		return this.m_USBKeyProcessor;
	}

	public void setM_USBKeyProcessor(KeyProcessor paramKeyProcessor) {
		this.m_USBKeyProcessor = paramKeyProcessor;
	}
}
